package com.zheng.hr.rpc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.hr.dao.mapper.HrPositionMapper;
import com.zheng.hr.dao.model.HrPersonalRecords;
import com.zheng.hr.dao.model.HrPosition;
import com.zheng.hr.rpc.api.HrPersonalRecordsService;
import com.zheng.hr.rpc.api.HrPositionService;
import com.zheng.oa.common.exception.OaException;
import com.zheng.upms.rpc.api.UpmsApiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * hr_position 岗位表 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-01-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class HrPositionServiceImpl extends BaseServiceImpl<HrPositionMapper, HrPosition> implements HrPositionService {
    @Autowired
    private HrPersonalRecordsService personalRecordsService;
    @Autowired
    private UpmsApiService apiService;

    @Override
    public boolean deletePositionByIds(String ids) throws OaException {
        EntityWrapper<HrPersonalRecords> personalRecordsEntityWrapper;
        EntityWrapper<HrPosition> positionEntityWrapper;
        String[] idArray = ids.split("-");
        for (String idStr : idArray) {
            Integer id = Integer.parseInt(idStr);
            positionEntityWrapper = new EntityWrapper<>();
            positionEntityWrapper.where("pid=" + id);
            if (selectCount(positionEntityWrapper) > 0) {
                throw new OaException("该岗位下有未删除的子岗位！");
            }
            personalRecordsEntityWrapper = new EntityWrapper<>();
            personalRecordsEntityWrapper.where("position_id=" + id);
            if (personalRecordsService.selectCount(personalRecordsEntityWrapper) > 0) {
                throw new OaException("该岗位下有未删除的人事信息！");
            }
            if (!deleteById(id)) {
                throw new OaException("删除岗位失败！");
            }
        }
        return true;
    }

    @Override
    public boolean insertPosition(HrPosition position) throws OaException {
        Integer pid = position.getPid();
        position.setCtime(new Date());
        String path = "";
        if (null == pid) {
            pid = 1;
            position.setPid(pid);
        }
        HrPosition parentPosition = super.selectById(pid);
        if (null != parentPosition) {
            path = parentPosition.getPath() + pid + ",";
        }
        position.setPath(path);
        return insert(position);
    }


    @Cacheable(cacheNames = "zheng-oa-rpc-service-ehcache", key = "'getAllPositionAndUserTree' +#username+#isUser")
    public JSONArray getAllPositionAndUserTree(Boolean isUser, String username) {
        Wrapper<HrPosition> positionEntityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(username)) {
            positionEntityWrapper = apiService.dataScopeFilter(positionEntityWrapper, username);
        }
        List<HrPosition> positionList = selectList(positionEntityWrapper);
        JSONArray treeArray = new JSONArray();
        for (HrPosition position : positionList) {
            JSONObject treeNode = new JSONObject();
            treeNode.put("id", position.getId());
            treeNode.put("nodeId", "positionId" + position.getId());
            Integer pid = position.getPid();
            if (pid != null) {
                treeNode.put("pId", position.getPid());
                treeNode.put("pNodeId", "positionId" + position.getPid());
            }
            treeNode.put("path", position.getPath());
            treeNode.put("name", position.getName());
            treeArray.add(treeNode);
        }
        if (isUser) {
            List<HrPersonalRecords> personalRecordsList = personalRecordsService.selectList(null);
            for (HrPersonalRecords personalRecords : personalRecordsList) {
                JSONObject treeNode = new JSONObject();
                treeNode.put("id", personalRecords.getId());
                treeNode.put("nodeId", "personalRecordsId" + personalRecords.getId());
                Integer positionId = personalRecords.getPositionId();
                if (positionId != null) {
                    treeNode.put("pId", positionId);
                    treeNode.put("pNodeId", "positionId" + positionId);
                }
                treeNode.put("name", personalRecords.getRealname());
                treeNode.put("isUser", true);
                treeArray.add(treeNode);
            }
        }
        return treeArray;
    }


}
